/**
 * Created with IntelliJ IDEA.
 * User: Tung An
 * Date: 12/11/13
 * Time: 1:09 PM
 * To change this template use File | Settings | File Templates.
 */


var MainPage = {

    isVisible: false,

    init: function () {
        this.getListLooks();
        this.show();
        MainPage.isVisible = true;
    },

    onClick: function () {
        switch (event.target.data) {
            case "edit":
                var parentNode = getParentNode(event.target, "look-item");
                window.location = "upload_image.jsp?look=" + parentNode.id + "&edit=true";
                break;
            case "look-item":
                var parentNode = getParentNode(event.target, "look-item");
                window.location = "/look_detail.jsp?look=" + parentNode.id;
                break;
            default :
                break;
        }
    },

    getListLooks: function () {
        var success = function (resp) {
            var userId = null;
            if ($class("username")[0] && $class("username")[0].id) {
                userId = $class("username")[0].id;
            }

            var html = xmlUtils.transformXSL(resp, "templates/looks.xsl", [
                {key: "userId", value: userId}
            ]);
            $id('list-look').innerHTML = "";
            $id('list-look').appendChild(html);
            setTimeout(function () {
                GridFormat.refreshLayout();
            }, 500);
        };

        var failure = function () {

        };

        ajax.post({
            url: "ProcessServlet",
            param: {action: "getall"},
            success: success,
            failure: failure
        });
    },

    show: function () {
        var listener = function (event) {
            var node = event.target;
            event.target.data = xmlUtils.getNodeWithData(node);
            MainPage.onClick(event);
        };
        $id("main-page").addEventListener("click", listener, false);
    }
};